import { Component, OnInit, ElementRef } from "@angular/core";
import { CreateService } from "../../services";
import { SrcType } from "../../../shared";

@Component({
  selector: "app-create-source",
  templateUrl: "./create-source.component.html",
  styleUrls: ["./create-source.component.scss"],
})
export class CreateSourceComponent implements OnInit {
  srcTypes$: SrcType[];
  selectedId: number;
  inputdata = [];

  constructor(private service: CreateService, private el: ElementRef) {}

  public ngOnInit(): void {
    // this.srcTypes$ = this.route.paramMap.pipe(
    //   switchMap(params => {
    //     // (+) before `params.get()` turns the string into a number
    //     this.selectedId = +params.get('id');
    //     return this.service.getSrcTypes();
    //   })
    // );
  }

  public onSearch(): void {
    let inputScreenId = this.el.nativeElement.querySelector("#inputScreenId")
      .value;
    this.service.getSrcTypes(inputScreenId).subscribe((data) => {
      this.srcTypes$ = data;
      if (this.srcTypes$.length === 0) {
        return;
      }
    });
  }

  private resource_type: string = "";
  private resource_class_type: string = "";
  private resource_calss_Pakage: string = "";

  public checkedRadio(i: string, j: string, k: string): void {
    this.resource_type = i;
    this.resource_class_type = j;
    this.resource_calss_Pakage = k;
  }

  public gotoCreateclass(): void {
    let inputScreenId = this.el.nativeElement.querySelector("#inputScreenId")
      .value;
    let classId = this.resource_type;
    let classType = this.resource_class_type;
    let classPakage = this.resource_calss_Pakage;
    this.service
      .createSource(inputScreenId, classId, classPakage, classType)
      .subscribe((data) => {
        const blob = new Blob([data.body], { type: "application/java" });
        const url = window.URL.createObjectURL(blob);
        const link = document.createElement("a");
        link.setAttribute("href", window.URL.createObjectURL(blob));
        link.setAttribute(
          "download",
          data.headers.get("Content-disposition").split("filename=")[1]
        );
        link.style.visibility = "hidden";
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
  }

  public gotoCreateJunit(): void {
    let inputScreenId = this.el.nativeElement.querySelector("#inputScreenId")
      .value;
    let classId = this.resource_type;
    let classType = this.resource_class_type;
    let classPakage = this.resource_calss_Pakage;
    if (classType !== "DTO") {
      alert("DTOを選んでください");
    } else {
      this.service
        .createJunit(inputScreenId, classId, classPakage, classType)
        .subscribe((data) => {
          const blob = new Blob([data.body], { type: "application/java" });
          const url = window.URL.createObjectURL(blob);
          const link = document.createElement("a");
          link.setAttribute("href", window.URL.createObjectURL(blob));
          link.setAttribute(
            "download",
            data.headers.get("Content-disposition").split("filename=")[1]
          );
          link.style.visibility = "hidden";
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        });
    }
  }
}
